#import "CallEndViewController.h"
#import "UserInfoLogic.h"
#import "EvaluateViewController.h"
@interface CallEndViewController ()<UserInfoLogicDelegate>
@property(nonatomic, strong) UIButton *closeButton;
@property(nonatomic, strong) UIImageView *bgview;
@property(nonatomic, strong) QMUIFillButton *attrButton;
@property(nonatomic, strong) QMUIFillButton *likeButton;
@property(nonatomic, strong) QMUIFillButton *evaluateButton;
@property(nonatomic, strong) UIImageView    *avatar;
@property(nonatomic, strong) UILabel *nickname;
@property(nonatomic, strong)  UserInfoLogic *logic;
@property(nonatomic, strong)  UserPageInfoModel *model;
@property(nonatomic, strong) UILabel *labTalkTime;
@property(nonatomic, strong) UILabel *labTakeCount;
@property(nonatomic, strong) UILabel *consumptionLable;
@property(nonatomic, strong) UILabel *callLable;
@property(nonatomic, strong) UIView *line1;
@end
@implementation CallEndViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    _logic = [UserInfoLogic new];
    _logic.uid = self.message.sender.id;
    _logic.delegagte = self;
    [_logic loadData];
    [[HUDHelper sharedInstance] syncLoading];
}
-(void)requestDataCompleted
{
    [[HUDHelper sharedInstance] syncStopLoading];
    _model = _logic.model;
    if([_model.attention isEqualToString:@"1"])
    {
        _attrButton.enabled = NO;
        [_attrButton setTitle:ASLocalizedString(@"已关注")forState:UIControlStateNormal];
    }
}
-(void)createUI
{
    self.closeButton = [[UIButton alloc] init];
    [self.closeButton setImage:IMAGE_NAMED(@"btn_close") forState:UIControlStateNormal];
    self.bgview = [[UIImageView alloc] init];
    self.bgview.contentMode = UIViewContentModeScaleAspectFill;
    [self.bgview sd_setImageWithURL:[cyurl safeUrl:_message.sender.avatar] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.bgview.image = [image imageByBlurSoft];
    }];
    [self.view addSubview:self.bgview];
    [self.view addSubview:self.closeButton];
    [self.closeButton addTarget:self action:@selector(handleCloseEvent:) forControlEvents:UIControlEventTouchUpInside];
    _attrButton =  [[QMUIFillButton alloc] initWithFillColor:MainColor titleTextColor:KWhiteColor];
    [_attrButton setTitle:ASLocalizedString(@"+ 关注")forState:UIControlStateNormal];
    [_attrButton addTarget:self action:@selector(doAttr) forControlEvents:UIControlEventTouchUpInside];
    _likeButton =  [[QMUIFillButton alloc] initWithFillColor:MainColor titleTextColor:KWhiteColor];
    [_likeButton setTitle:ASLocalizedString(@"给Ta点赞")forState:UIControlStateNormal];
    [_likeButton addTarget:self action:@selector(doLike) forControlEvents:UIControlEventTouchUpInside];
    _evaluateButton =  [[QMUIFillButton alloc] initWithFillColor:MainColor titleTextColor:KWhiteColor];
    [_evaluateButton addTarget:self action:@selector(Evaluate) forControlEvents:UIControlEventTouchUpInside];
    [_evaluateButton setTitle:ASLocalizedString(@"评价")forState:UIControlStateNormal];
    [self.view addSubview:_evaluateButton];
    [_attrButton addTapBlock:^(UIButton *btn) {
    }];
    [self.view addSubview:_attrButton];
    [self.view addSubview:_likeButton];
    self.avatar = [[UIImageView alloc] init];
    [self.avatar sd_setImageWithURL:[cyurl safeUrl:self.message.sender.avatar]];
    ViewRadius(self.avatar, 50);
    [self.view addSubview:self.avatar];
    self.nickname = [[UILabel alloc] init];
    self.nickname.textAlignment = NSTextAlignmentCenter;
    self.nickname.textColor = [UIColor whiteColor];
    self.nickname.font = DEFAULT_BOLDFONT(18);
    self.nickname.text = self.message.sender.user_nickname;
    [self.view addSubview:self.nickname];
    self.labTalkTime = [[UILabel alloc] init];
    self.labTalkTime.text = self.talkTime;
    self.labTalkTime.textColor = [UIColor whiteColor];
    self.labTalkTime.font = DEFAULT_BOLDFONT(22);
    [self.view addSubview:self.labTalkTime];
    self.labTakeCount = [[UILabel alloc] init];
    self.labTakeCount.textColor = KWhiteColor;
    self.labTakeCount.text = [NSString stringWithFormat:@"%ld%@",(long)self.callCount,KCOIN_NAME];
    self.labTakeCount.font = DEFAULT_BOLDFONT(22);
    self.consumptionLable = [[UILabel alloc] init];
    self.callLable = [[UILabel alloc] init];
    self.consumptionLable.text = ASLocalizedString(@"消费");
    self.callLable.text = ASLocalizedString(@"通话时间");
    self.consumptionLable.textAlignment = NSTextAlignmentCenter;
    self.callLable.textAlignment = NSTextAlignmentCenter;
    self.consumptionLable.textColor = KWhiteColor;
    self.callLable.textColor = KWhiteColor;
    self.callLable.font = DEFAULT_BOLDFONT(22);
    self.consumptionLable.font = DEFAULT_FONT(22);
    [self.view addSubview:self.callLable];
    [self.view addSubview:self.consumptionLable];
    self.line1 = [[UIView alloc] init];
    self.line1.backgroundColor = KWhiteColor;
    [self.view addSubview:self.line1];
    [self.view addSubview:self.labTakeCount];
    [self setLayout];
}
- (void)Evaluate {
    EvaluateViewController *evaluate = [[EvaluateViewController alloc] init];
    evaluate.to_user_id = self.message.sender.id;
    evaluate.channel_id = self.message.channel;
    evaluate.view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.3];
    evaluate.definesPresentationContext = YES;
    evaluate.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self presentViewController:evaluate animated:NO completion:nil];
}
-(void)doLike
{
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"user_api" A:@"fabulous"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:_model.id forKey:@"to_user_id"];
    [[HUDHelper sharedInstance] syncLoading:ASLocalizedString(@"正在点赞")];
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        [[HUDHelper sharedInstance] syncStopLoading];
        [MBProgressHUD showTopTipMessage:ASLocalizedString(@"点赞成功!")];
        [self handleCloseEvent:nil];
    } failure:^(NSString *error) {
        [[HUDHelper sharedInstance] syncLoading:ASLocalizedString(@"失败")];
    } hasCache:NO];
}
-(void)doAttr
{
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"personal_api" A:@"click_attention"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:_model.id forKey:@"id"];
    [[HUDHelper sharedInstance] syncLoading:ASLocalizedString(@"正在关注")];
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        [[HUDHelper sharedInstance] syncStopLoading];
        if([_model.attention isEqualToString:@"1"])
        {
            _model.attention = @"2";
            [_attrButton setTitle:ASLocalizedString(@"+ 关注")forState:UIControlStateNormal];
            [_attrButton setFillColor:MainColor];
        }
        else
        {
            _model.attention = @"1";
            [_attrButton setTitle:ASLocalizedString(@"已关注")forState:UIControlStateNormal];
            [_attrButton setFillColor:CLightGray];
            _attrButton.enabled = NO;
        }
    } failure:^(NSString *error) {
        [[HUDHelper sharedInstance] syncLoading:ASLocalizedString(@"失败")];
    } hasCache:NO];
}
-(void)handleCloseEvent:(UIButton *)button
{
    if([self.delegate respondsToSelector:@selector(CallEndCloseView)])
    {
        [self dismissViewControllerAnimated:NO completion:^{
            [self.delegate CallEndCloseView];
        }];
    }
}
-(void)setLayout
{
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20);
        make.top.equalTo(self.view).offset(25);
        make.width.and.height.equalTo(@60);
    }];
    [self.bgview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.attrButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-60);
        make.width.equalTo(self.view).offset(-90);
        make.height.equalTo(@45);
        make.centerX.equalTo(self.view);
    }];
    [self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.evaluateButton.mas_top).offset(-10);
        make.width.equalTo(self.view).offset(-90);
        make.height.equalTo(@45);
        make.centerX.equalTo(self.view);
    }];
    [_evaluateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.attrButton.mas_top).offset(-10);
        make.width.equalTo(self.view).offset(-90);
        make.height.equalTo(@45);
        make.centerX.equalTo(self.view);
    }];
    [self.avatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(80);
        make.height.and.width.equalTo(@100);
    }];
    [self.nickname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatar.mas_bottom).offset(10);
        make.centerX.equalTo(self.view);
    }];
    [self.consumptionLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).multipliedBy(0.5);
        make.height.equalTo(@30);
        make.left.equalTo(self.view);
        make.top.equalTo(self.nickname.mas_bottom).offset(30);
    }];
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@50);
        make.width.equalTo(@2);
        make.right.equalTo(self.consumptionLable);
        make.top.equalTo(self.consumptionLable).offset(5);
    }];
    [self.callLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).multipliedBy(0.5);
        make.height.equalTo(@30);
        make.right.equalTo(self.view);
        make.top.equalTo(self.nickname.mas_bottom).offset(30);
    }];
    [self.labTalkTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.callLable.mas_bottom).offset(10);
        make.centerX.equalTo(self.callLable);
    }];
    [self.labTakeCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.consumptionLable.mas_bottom).offset(10);
        make.centerX.equalTo(self.consumptionLable);
    }];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
